#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

class String
{
public:
	String()
    :_pstr(new char[1]())
    ,_capacity(1)
    {
        cout << "String()" << endl;
    };

	String(const char * pstr)
    :_capacity(strlen(pstr) + 1)
    ,_pstr(new char[_capacity]())
    {
        strcpy(_pstr,pstr);
        cout << "String(const char* pstr)" << endl;
    };

    //用另一个字符串初始化
	String(const String & rhs)
    :_capacity(strlen(rhs._pstr) + 1)
    ,_pstr(new char[_capacity]())
    {
        cout << "String(const String & rhs)" << endl;
        strcpy(_pstr,rhs._pstr);
    };

    ~String(){
        cout << "~String()" << endl;
        if(_pstr){
            delete _pstr;
            _pstr = nullptr;
        }

    void print() {
        if(_pstr){
            cout << _pstr << endl;
        }else{
            cout << endl;
        }
    }
private:
	char * _pstr;
    size_t _capacity;
};

void test0(){
    String str;
    String str0("miaomiao");
    String str1 = str0;

    str0.print();
    str1.print();

    String str2 = "dawang";
    str2.print();



    //cout << str1[0] << endl;
}


int main(int argc, char * argv[])
{
    test0();
    return 0;
}

